package com.hang.controller;

import com.hang.common.Result;
import com.hang.model.Clazz;
import com.hang.model.Leave;
import com.hang.model.Task;
import com.hang.service.*;
import com.hang.vo.LeaveVo;
import com.hang.vo.LeavesDTO;
import com.hang.vo.TaskDetail;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("student")
@Api(tags = "学生端接口")
@RequiredArgsConstructor
public class StudentController {
    private final ClazzService clazzService;

    private final AttendanceService attendanceService;

    private final LeaveService leaveService;

    private final TaskService taskService;

    private final StudentService studentService;

    private final FileService fileService;

    @PostMapping("punchTheClock/{studentId}/{taskId}")
    @ApiOperation(value = "学生打卡")
    public Result punch(
            @PathVariable("studentId") String studentId,
            @PathVariable("taskId") Long taskId
    ){
        Boolean success = attendanceService.punchClock(studentId,taskId);
        if(success){
            attendanceService.recordPunchTime(studentId,taskId);
            return Result.ok().message("打卡成功");
        }
        return Result.fail().message("打卡失败");
    }

    @GetMapping("getPosition/{taskId}")
    @ApiOperation(value = "返回任务所处的经纬度信息")
    public Result getPosition(
        @PathVariable("taskId") Long taskId
    ){
        Map<String,Object> map = taskService.getPosition(taskId);
        return Result.ok(map);
    }

    @GetMapping("viewAll/{studentId}/{page}/{size}")
    @ApiOperation(value = "分页获取班级列表")
    public Result getClazzListByStudent(
            @PathVariable("studentId") String studentId,
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size
    ){
        List<Clazz> clazzList = studentService.getHisList(studentId,page,size);

        //判断是否还有数据
        if(studentService.hasMoreData(studentId,page,size)){
            return Result.ok(clazzList,Boolean.TRUE);
        }
        return Result.ok(clazzList,Boolean.FALSE);
    }

    @GetMapping("info/getClazzByID/{clazzId}")
    @ApiOperation(value = "获取班级的详尽信息")
    public Result getClazzInfoDetail(
            @PathVariable("clazzId") String clazzId
    ){
        //先获取班级的详尽信息
        Map<String,Object> map = clazzService.detail(clazzId);
        return Result.ok(map);
    }

    @GetMapping("info/getResult/{studentId}")
    @ApiOperation(value = "获取自己所有的考勤任务列表")
    public Result getTaskList(
            @PathVariable("studentId") String studentId
    ){
        List<TaskDetail> tasks = taskService.getTasksByStudentId(studentId);

        //判断是否还有数据
        return Result.ok(tasks);
    }

    @GetMapping("info/getResultByClazz/{studentId}/{clazzId}/{page}/{size}")
    @ApiOperation(value = "获取自己所有的考勤任务列表")
    public Result getTaskListByClazz(
            @PathVariable("studentId") String studentId,
            @PathVariable("clazzId") String clazzId,
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size
    ){
        List<TaskDetail> tasks = taskService.getTasksByStudentIdAndClazzId(studentId,clazzId,page,size);
        //判断是否还有数据
        if(taskService.hasMoreData(clazzId,page,size)){
            return Result.ok(tasks,Boolean.TRUE);
        }
        return Result.ok(tasks,Boolean.FALSE);
    }

    @PostMapping("levelApply")
    @ApiOperation(value = "请假申请")
    //TODO 目前只能上传一张图片，后面会拓展成上传多张图片
    public Result levelApply(
            @RequestBody LeaveVo leaveVo,
            @RequestParam(value = "file",required = false) MultipartFile multipartFile
    ){
        //图片上传
        String url = fileService.upload(multipartFile);

        Map<String,Object> map = leaveService.apply(leaveVo,url);

        return Result.ok(map);
    }


    @GetMapping("getPunchTime/{taskId}")
    @ApiOperation(value = "获取学生的打卡时间")
    public Result getPunchTime(
            @PathVariable("taskId") Long taskId
    ){
        Map<String,Object> map = attendanceService.getPunchTime(taskId);
        return Result.ok(map);
    }

    @GetMapping("getMyLeaves/{studentId}/{page}/{size}")
    @ApiOperation(value = "获取学生自己的请假记录")
    public Result getMyLeaves(
            @PathVariable("studentId") String studentId,
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size
    ){
        List<LeavesDTO> leaves = leaveService.getMyLeaves(studentId,page,size);

        if(leaveService.hasMoreStudentData(studentId,page,size)){
            return Result.ok(leaves,Boolean.TRUE);
        }
        return Result.ok(leaves,Boolean.FALSE);
    }

    @GetMapping("getStatusByTask/{studentId}/{taskId}")
    @ApiOperation(value = "获取任务的打卡状态")
    public Result getTaskStatus(
            @PathVariable("studentId") String studentId,
            @PathVariable("taskId") Long taskId
    ){
        Map<String,Object> map = taskService.getTaskStatus(studentId,taskId);
        return Result.ok(map);
    }
}